package com.kk.pojo.dto;

import com.kk.pojo.entity.SysUser;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serial;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author kl
 * @version 1.0
 * @description: TODO
 * @date 2025/10/16 下午3:45
 */
@NoArgsConstructor
@Data
public class LoginUser implements UserDetails {
    @Serial
    private static final long serialVersionUID = 1L;

    private BigInteger userId;

    private String username;

    private String password;

    private Set<String> permissions;

    private Set<String> roles;

    private SysUser sysUser;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> authorities =permissions.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toSet());

        roles.stream()
                .map(SimpleGrantedAuthority::new)
                .forEach(authorities::add);
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return sysUser == null || sysUser.getStatus() == 1;
    }
}
